﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL.Interfaces;
using Microsoft.Practices.ServiceLocation;
using POCOS;

namespace MagazineWebForms.BusinessMethodes
{
    public static class BusinessMethods
    {
        private static IAuteurServices _auteurSerices;
        private static IUserProfileServices _userProfiles;

        public static object GetAuhtors()
        {
            _auteurSerices = ServiceLocator.Current.GetInstance<IAuteurServices>();
           
            var auteurs = _auteurSerices.GetAuteurs().Select(aut => new { aut.Id, aut.Name, aut.Lastname }).ToList();

            return auteurs;
        }
        public static object UsersList(int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                //Get data from database
                _userProfiles = ServiceLocator.Current.GetInstance<IUserProfileServices>();

                int usersCount = _userProfiles.GetUserProfile().Count();
               
                List<UserProfile> Users = GetUsers(jtStartIndex, jtPageSize, jtSorting);

                //Return result to jTable
                return new { Result = "OK", Records = Users, TotalRecordCount = usersCount };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

       
        
        public static List<Auteur> GetListAuhtors()
        {
            _auteurSerices = ServiceLocator.Current.GetInstance<IAuteurServices>();
            var auteurs = _auteurSerices.GetAuteurs();

            return auteurs;
        }

        //Get Authors 
        public static List<UserProfile> GetUsers(int startIndex, int count, string sorting)
        {
            _userProfiles = ServiceLocator.Current.GetInstance<IUserProfileServices>();

            IEnumerable<UserProfile> query = _userProfiles.GetUserProfile();

            //Sorting
            //This ugly code is used just for demonstration.
            //Normally, Incoming sorting text can be directly appended to an SQL query.
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("UserName ASC"))
            {
                query = query.OrderBy(p => p.UserName);
            }
            else if (sorting.Equals("Name DESC"))
            {
                query = query.OrderByDescending(p => p.UserName);
            }


            if (string.IsNullOrEmpty(sorting) || sorting.Equals("LastName ASC"))
            {
                query = query.OrderBy(p => p.Mail);
            }
            else if (sorting.Equals("Mail DESC"))
            {
                query = query.OrderByDescending(p => p.Mail);
            }

           


            else
            {
                query = query.OrderBy(p => p.UserName); //Default!
            }

            return count > 0
                       ? query.Skip(startIndex).Take(count).ToList() //Paging
                       : query.ToList(); //No paging
        }
    }
}